
import delimited "${data2}mw_2020.csv",clear
tempfile 2020
save `2020'

use  "${data2}mw_state_annual.dta",clear
rename statefips state_fips
rename  mean_mw mw

* rename mw mw_1
* use "${data2}nominalmw_annual_withlags.dta",clear

merge m:1 state_fips using "${data2}state_geocodes.dta", nogen keep(3)
merge m:1 state_fips year using "${data2}state_pop_seer.dta", nogen keep(3)
keep state_fips year mw state_name state_abb division region pop
label var pop "state population"

merge 1:1 year state_fips using "${data2}wage_high_edu.dta", nogen
merge 1:1 year state_fips using "${data2}high_edu_ur.dta", nogen
merge 1:1 year state_fips using "${data2}hpi.dta", nogen keep(3)
merge m:1 year using "${data2}cpi_fred.dta", nogen keep(3)
gen mw_real = 100*mw/cpi
xtset state_fips year
gen chpi=d1.hpi / hpi

tempfile mw
save `mw'

keep if year ==2019
replace year = 2020
merge 1:1 state_name using `2020', nogen keep(3)
append using `mw'

* merge m:1 year using "${data2}cpi_fred.dta", nogen keep(1 3)
* merge 1:1 state_fips year using "${data2}aca_expansion_eitc.dta" , nogen keep(1 3)
replace year = year +1
keep if year >=2010
tab year
xtset state_fips year
xtset state_fips year


gen mw_change=d1.mw
* gen mw_change=d1.mw_real
drop if mw_change==.
keep if ur_high_edu != .

gen byte mw100 = mw_change >=1
by state_fips:replace mw100 = sum(mw100)
replace mw100=1 if mw100>1
lab var mw100 "mw change >=$1"

gen byte mw75 = mw_change >.75
by state_fips:replace mw75 = sum(mw75)
replace mw75=1 if mw75>1
lab var mw75 "mw change >=$0.75"

gen byte mw50 = mw_change >=.5
by state_fips:replace mw50 = sum(mw50)
replace mw50=1 if mw50>1
lab var mw50 "mw change >=$0.50"

gen byte mw25 = mw_change >=.25 & mw_change <1
by state_fips:replace mw25 = sum(mw25)
replace mw25=1 if mw25>1
lab var mw25 "mw change >=$0.25"

tempfile mw
save `mw'

local dems 1/8
local dem 2

forvalues dem = `dems' {
* foreach dem in local dems {
use "${data1}regready_cps_ipums_84.dta", clear


local thresholdlist .5  1 1.5 2 3 4
* local thresholdlist 4
replace EF1 = 0

*reg
foreach j of numlist .5  1 1.5 2 3  {
local d = `j'*100
g  under_`d' = contpov < `j'
}
replace asecwt = asecwt/100000
keep if dem`dem'==1
collapse under_* (sum) asecwt , by(state_fips year)

foreach j of numlist .5  1 1.5 2 3  {
local d = `j'*100
label var under_`d' "Pr(INR < `j') "
}


merge 1:1 state year using `mw', nogen keep(3)
gen dem = `dem'
tempfile d`dem'
save `d`dem''
}

clear
forvalues dem = `dems' {
  append using `d`dem''
}

save "${data1}state_panel_mw_poverty.dta", replace
gen fips = state_fips
drop state_fips
rename fips state_fips

export delimited "${data1}state_panel_mw_poverty.csv", replace
tab  year
tab state_abb if mw100==1
